package com.nhjf.common.util.tools;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 加解密类
 * @author huangjingying
 * @company 浙江汇购科技有限公司
 * 2012-4-12
 * sql-webServer
 * com.nhjf.common.util.tools
 * @version 1.0
 *
 */
public class DESUtil {
	public static final String KEY_STRING = "farawayfrom";//生成密钥的字符串  
	static Key key;

	/**
	 * 将 s 进行 BASE64 编码
	 * @param s
	 * @return
	 */
	public static String getBASE64(String s) {
		if (s == null)
			return null;
		return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
	}

	/**
	 * 将 BASE64 编码的字符串 s 进行解码
	 * @param s
	 * @return
	 */
	public static String getFromBASE64(String s) {
		if (s == null)
			return null;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] b = decoder.decodeBuffer(s);
			return new String(b);
		} catch (Exception e) {
			return null;
		}
	}

	/**    
	 * 根据参数生成KEY    
	 *     
	 * @param strKey    
	 */
/*	public static void getKey(String strKey) {
		try {
			KeyGenerator _generator = KeyGenerator.getInstance("DES");
			_generator.init(new SecureRandom(strKey.getBytes()));
			key = _generator.generateKey();
			_generator = null;
		} catch (Exception e) {
			LogMessage.logwrite(DESUtil.class, e.getMessage());
		}
	}*/
	public static void getKey(String strKey) {
		try {
			KeyGenerator _generator = KeyGenerator.getInstance("DES");
			SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
			secureRandom.setSeed(strKey.getBytes());
			_generator.init(secureRandom);
			key = _generator.generateKey();
			_generator = null;
		} catch (Exception e) {
			LogMessage.logwrite(DESUtil.class, e.getMessage());
		}
	}

	/**    
	 * 加密String明文输入,String密文输出    
	 *     
	 * @param strMing    
	 * @return    
	 */
	public static String getEncString(String strMing) {
		DESUtil.getKey(KEY_STRING);// 生成密匙       
		byte[] byteMi = null;
		byte[] byteMing = null;
		String strMi = "";
		BASE64Encoder base64en = new BASE64Encoder();
		try {
			byteMing = strMing.getBytes("UTF8");
			byteMi = getEncCode(byteMing);
			strMi = base64en.encode(byteMi);
		} catch (Exception e) {
			LogMessage.logwrite(DESUtil.class, e.getMessage());
		} finally {
			base64en = null;
			byteMing = null;
			byteMi = null;
		}
		return strMi;
	}

	/**    
	 * 解密 以String密文输入,String明文输出    
	 *     
	 * @param strMi    
	 * @return    
	 */
	public static String getDesString(String strMi) {
		DESUtil.getKey(KEY_STRING);// 生成密匙       
		BASE64Decoder base64De = new BASE64Decoder();
		byte[] byteMing = null;
		byte[] byteMi = null;
		String strMing = "";
		try {
			byteMi = base64De.decodeBuffer(strMi);
			byteMing = getDesCode(byteMi);
			strMing = new String(byteMing, "UTF8");
		} catch (Exception e) {
			LogMessage.logwrite(DESUtil.class, e.getMessage());
		} finally {
			base64De = null;
			byteMing = null;
			byteMi = null;
		}
		return strMing;
	}

	/**    
	 * 加密以byte[]明文输入,byte[]密文输出    
	 *     
	 * @param byteS    
	 * @return    
	 */
	private static byte[] getEncCode(byte[] byteS) {
		byte[] byteFina = null;
		Cipher cipher;
		try {
			cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byteFina = cipher.doFinal(byteS);
		} catch (Exception e) {
			LogMessage.logwrite(DESUtil.class, e.getMessage());
		} finally {
			cipher = null;
		}
		return byteFina;
	}

	/**    
	 * 解密以byte[]密文输入,以byte[]明文输出    
	 *     
	 * @param byteD    
	 * @return    
	 */
	private static byte[] getDesCode(byte[] byteD) {
		Cipher cipher;
		byte[] byteFina = null;
		try {
			cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE, key);
			byteFina = cipher.doFinal(byteD);
		} catch (Exception e) {
			LogMessage.logwrite(DESUtil.class, e.getMessage());
		} finally {
			cipher = null;
		}
		return byteFina;

	}

	public static void main(String[] args) {

		/*  String strEnc = DESUtil.getEncString("yanfa");// 加密字符串,返回String的密文       
		    System.out.println(strEnc);       
		  
		    String strDes = DESUtil.getDesString("kSElf8soueY=");// 把String 类型的密文解密        
		    System.out.println(strDes);
		    
		    String strDesUserName = DESUtil.getDesString("kSElf8soueY=");// 把String 类型的密文解密        
		    System.out.println(strDes);   
		*/

		//先将用户名密码用des加密，把加密后的字串放在properties里
//		String strEncUserName = DESUtil.getEncString("sa");// 加密字符串,返回String的密文       
//		System.out.println("sa用户名加密后" + strEncUserName);
//
//		String strEncUserName2 = DESUtil.getEncString("wms2012");// 加密字符串,返回String的密文       
//		System.out.println("wms2012用户名加密后" + strEncUserName2);
//
//		String strEncPassword = DESUtil.getEncString("p@ssword");// 加密字符串,返回String的密文       
//		System.out.println("p@ssword密码加密后" + strEncPassword);
//
//		String strEncPassword2 = DESUtil.getEncString("czshop");// 加密字符串,返回String的密文       
//		System.out.println("czshop密码加密后" + strEncPassword2);
//		//url采取base64编码
//		String strbaseUrl = DESUtil.getBASE64("jdbc:sqlserver://192.168.11.139:1433;databaseName=nanhang");
//		System.out.println("url加密后"+strbaseUrl);
//		System.out.println("url解密后"+DESUtil.getFromBASE64(strbaseUrl));
//	
//		String strbaseUrl2 = DESUtil.getBASE64("jdbc:oracle:thin:@192.168.2.101:1521:bb2c");
//		System.out.println("url2加密后"+strbaseUrl2);
//		System.out.println("url解密后"+DESUtil.getFromBASE64(strbaseUrl2));
//
//		//本机url采取base64编码
//		String strbaseUrl3 = DESUtil.getBASE64("jdbc:sqlserver://127.0.0.1:1433;databaseName=czshop");
//		System.out.println("url加密后"+strbaseUrl3);
//		System.out.println("url解密后"+DESUtil.getFromBASE64(strbaseUrl3));
//		String strEncPassword3 = DESUtil.getEncString("Google@511*baiDu__OMG");// 加密字符串,返回String的密文       
//		System.out.println("czshop密码加密后" + strEncPassword3);
		
		//143数据库
		
		//String strbaseUrl4 = DESUtil.getBASE64("jdbc:sqlserver://192.168.2.212:1433;databaseName=test");
		//System.out.println("url nanhang加密后1="+strbaseUrl4);
		//System.out.println("url nanhang解密后="+DESUtil.getFromBASE64("amRiYzpzcWxzZXJ2ZXI6Ly8xOTIuMTY4LjIuMjExOjE0MzM7ZGF0YWJhc2VOYW1lPWN6c2hvcA=="));
		//String strEncPassword4 = DESUtil.getEncString("nanhang@123");// 加密字符串,返回String的密文       
		//System.out.println("p@ssword密码加密后" + strEncPassword4);
		//System.out.println(DESUtil.getDesString("kWPA3I5dD34ac5Uvc871zw=="));
		
		//本机
		String strbaseUrl4 = DESUtil.getBASE64("jdbc:sqlserver://192.168.101.135:1433;databaseName=czshop");
		System.out.println("url nanhang加密后1="+strbaseUrl4);
		String strEncUserName = DESUtil.getEncString("sa");// 加密字符串,返回String的密文       
		System.out.println("sa用户名加密后" + strEncUserName);		
		String strEncPassword = DESUtil.getEncString("zzqs432");// 加密字符串,返回String的密文       
		System.out.println("p@ssword密码加密后" + strEncPassword);
		
		//System.out.println("url 212解密后="+DESUtil.getFromBASE64("amRiYzpzcWxzZXJ2ZXI6Ly8xOTIuMTY4LjIuMjEyOjE0MzM7ZGF0YWJhc2VOYW1lPW5hbmhhbmc="));
		//System.out.println("username 212解密后="+DESUtil.getDesString("/dxovGkdLFQ="));
		//System.out.println("pw 212解密后="+DESUtil.getDesString("kWPA3I5dD34ac5Uvc871zw=="));
			
		
		//2.143 czsafe数据库
		
//				String strbaseUrl5 = DESUtil.getBASE64("jdbc:microsoft:sqlserver://192.168.2.143:1433;SelectMethod=cursor;databaseName=czsafe");
//				System.out.println("url safe加密后"+strbaseUrl5 + "end");
//				System.out.println("url safe解密后"+DESUtil.getFromBASE64(strbaseUrl5));
//				String strEncPassword5 = DESUtil.getEncString("Skiller135");// 加密字符串,返回String的密文       
//				System.out.println("p@ssw0rd密码加密后" + strEncPassword5);
//				
//				String pw = "SQdp9VOw65mhrH7UoLO4rctry4vhSEGl";
//				String pw2 = "lMseJ5H1JgBQdZuCqmTigw==";
//				System.out.println("pw:" + DESUtil.getDesString(pw2));
	}
}
